package main

import (
	"encoding/hex"
	"log"
	"time"

	"gitee.com/general252/ebml-go/core/codecs"
	"gitee.com/general252/goffmpeg"
	"github.com/bluenviron/mediacommon/pkg/codecs/h264"
)

func init() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
}

func main() {

	var (
		err      error
		filename = "F:/FFOutput/tiancai.mp4"
		pkt      *goffmpeg.Packet

		videoIndex = 0
		audioIndex = 1
	)
	de := goffmpeg.NewDemuxer(filename)
	// 打开文件
	if err = de.Open(); err != nil {
		log.Println(err)
		return
	}

	for {
		time.Sleep(time.Millisecond * 2)

		// 读取数据
		if pkt, err = de.ReadPacket(); err != nil {
			log.Println(err)
			break
		}

		if pkt.StreamIndex == videoIndex {
			codecs.EmitNALUH264Data(pkt.Data, true, func(t h264.NALUType, data []byte) {
				log.Printf("%6v %6v %v %6v | %v", pkt.PTS, pkt.DTS, pkt.Duration, t, hex.Dump(data[:6]))
			})
		} else if pkt.StreamIndex == audioIndex {
		}
	}

	// 关闭
	de.Close()
}
